package com.wframe.project.former.web.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.frame.database.JdataBean;
import com.frame.pageform.DataPageForm;
import com.frame.pageform.annotation.Login;
import com.frame.util.EncryptUtil;
import com.wframe.framework.spring.SpringContextHolder;
import com.wframe.project.former.dataservices.user.UserService;

import com.wframe.project.jnd.extinterface.sms.SmsUtil;
import com.wframe.project.jnd.services.impl.UserServiceImpl;
import com.wframe.project.jnd.util.WebUtils;

@Login
public class UserPage extends DataPageForm{
	
	private JdataBean bean;
	private JdataBean company;
	UserService userService=UserService.getIntance();

	private com.wframe.project.jnd.services.UserService wbuserService = SpringContextHolder.getBean(com.wframe.project.jnd.services.UserService.class);

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		setPagination(userService.queryUser(bean, getPageSize_(), getPageNum()));
		return "/webadmin/user/userList.jsp";
	}

	@Override
	public String show() throws Exception {

		// TODO Auto-generated method stub
		if(bean!=null){
			bean=userService.getBean(bean.getString("id"));
		}
		if(bean==null){
			return "/webadmin/user/userAdd.jsp";
		}
		company=userService.getCompanyedByUid(bean.getString("id"));
		if(company!=null)
			getRequest().setAttribute("qullist", userService.querycCompanyQul(company.getString("id")));
		return "/webadmin/user/userInfo.jsp";
	}

	public String enabledUser() throws Exception{
		userService.enabledUser(bean.getString("id"));
		return getJsonObject().toString();
	}
	public String disabledUser() throws Exception{
		userService.disabledUser(bean.getString("id"));
		return getJsonObject().toString();
	}
	public String editUserPassword() throws Exception{
		String password  = bean.getString("password");
		userService.editUserPassword(bean.getString("id"), EncryptUtil.md5Encode(password));
		return getJsonObject().toString();
	}
	@Override
	public String toDel() throws Exception {
		// TODO Auto-generated method stub
		userService.delUser(bean.getString("id"));
		return getJsonObject().toString();
	}

	@Override
	public boolean toUpdate() throws Exception {

		boolean addUser = false;
		String uid = bean.getString("id");
		if( uid == null ){
			addUser = true;
		}

		if( addUser ) {
			JdataBean addUserBean = wbuserService.registerByAccount(bean.getString("loginname"), bean.getString("phone"), bean.getString("loginpwd"), com.wframe.project.jnd.services.UserService.UserSourceEnum.BACKSTAGE.ordinal());
			if(addUserBean==null||addUserBean.get("userid")==null){
				bean = null;
				return false;
			}
			String userId = WebUtils.aesDecrypt(addUserBean.get("userid").toString());
			bean.remove("loginname");
			bean.remove("phone");
			bean.remove("loginpwd");
			bean.put("id",userId);
		} else {
			bean.remove("authstate");
		}

		// TODO Auto-generated method stub
		Map key=new HashMap();
		key.put("id", bean.getString("id"));
		userService.update(bean.getDataMap(), key);

		if( addUser ){
			if( bean.get("authstate") != null ){
				company.put("state",bean.get("authstate"));
			} else {
				company.put("state","1");
			}
		}

		if(company.get("region")==null){
			company.put("region","");
		}

		JdataBean gcompany=userService.getCompanyByUid(bean.getString("id"));
		if( gcompany != null ) {
			company.put("id", gcompany.getString("id"));
		} else {
			company.put("userid", bean.getString("id"));
		}
		userService.saveCompany(company);
		getJsonObject().put("state", "ok");
		getJsonObject().getJsonPage().closeWin("ok");
		return false;
	}
	
	/**
	 * 企业认证管理
	 * @return
	 * @throws Exception 
	 */
	public String comauthList() throws Exception{
		setPagination(userService.queryAuth(bean, getPageSize_(), getPageNum()));
		return "/webadmin/user/companyAuthList.jsp";
	}
	public String showCompanyAuth() throws Exception{
		bean=userService.getCompany(bean.getString("id"));
		if(bean!=null)
			getRequest().setAttribute("qullist", userService.querycCompanyQul(bean.getString("id")));
		return "/webadmin/user/companyAuthShow.jsp";
	}
	public String authPass() throws Exception{
		userService.authPass(bean.getString("userid"),bean.getString("id"));
		getJsonObject().put("state", "ok");
		JdataBean user=userService.getBean(bean.getString("userid"));
		new SmsUtil().sendApproveSuccess(user.getString("phone"), user.getString("title"));
		return getJsonObject().toString();
	}
	public String authFail() throws Exception{
		userService.authFail(bean.getString("userid"),bean.getString("id"),bean.getString("content"));
		getJsonObject().put("state", "ok");
		JdataBean user=userService.getBean(bean.getString("userid"));
		new SmsUtil().sendApproveFailure(user.getString("phone"), user.getString("title"));
		getJsonObject().getJsonPage().closeWin("ok");
		return getJsonObject().toString();
	}
	
	public String userselQuery() throws Exception{
		setPagination(userService.queryUser(bean, getPageSize_(), getPageNum()));
		return "/webadmin/user/userSel.jsp";
	}

	/**
	 * 用户反馈
	 * @return
	 */
	public String feedbackList() throws Exception{
		setPagination(userService.queryUserFeedBack(bean,getPageSize_(),getPageNum()));
		return "/webadmin/user/feedbackList.jsp";
	}
	//查看反馈详情
	public String showFeedBackList() throws Exception {
		bean = userService.getFeedBackById(bean.getString("id"));
		if (bean != null){
			List<JdataBean> photodataid = userService.queryPicturesDataid(bean.getString("photodataid"));
			getRequest().setAttribute("aplist",photodataid);
		}

		return "/webadmin/user/feedbackShow.jsp";
	}

	public JdataBean getBean() {
		return bean;
	}

	public void setBean(JdataBean bean) {
		this.bean = bean;
	}


	public JdataBean getCompany() {
		return company;
	}


	public void setCompany(JdataBean company) {
		this.company = company;
	}
	
}
